<template>
  <!-- 付款添加 -->
  <div class="contractInfo box">
    <el-divider content-position="left">{{
      $route.query.mode == "add" ? "添加付款" : "编辑付款"
    }}</el-divider>
    <div class="formBox">
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="付款名称" prop="title">
          <el-input
            v-model="form.title"
            clearable
            placeholder="请输入付款名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="付款类型" prop="type" class="priceItem">
          <el-select
            v-model="form.type"
            placeholder="请选择付款类型"
            style="width: 100%"
            clearable
            @change="cahngeType"
          >
            <el-option label="供应商" :value="1" />
            <el-option label="工资" :value="2" />
            <el-option label="日常开支" :value="3" />
            <el-option label="其他" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="vendorData" v-if="form.type == 1">
          <!-- <el-input
            v-model="form.supplierName"
            clearable
            placeholder="请选择供应商"
          >
            <template #append>
              <el-button @click="showSelectVendor">选择</el-button>
            </template>
          </el-input> -->
          <sc-table-select
            v-model="form.vendorData"
            :apiObj="vendorApiObj"
            style="width: 100%"
            table-width="100%"
            clearable
            collapse-tags
            collapse-tags-tooltip
            @change="vendorChange"
            :props="vendorProps"
          >
            <template #header="{ form, submit }">
              <el-form :inline="true" :model="form">
                <el-form-item>
                  <el-input
                    v-model="form.search"
                    placeholder="供应商名称"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-button type="primary" @click="submit">查询</el-button>
              </el-form>
            </template>
            <el-table-column
              label="供应商名称"
              prop="vendorName"
              min-width="150"
            ></el-table-column>
            <el-table-column
              label="应付金额"
              prop="needTotlaVendorMoney"
              width="200"
            ></el-table-column>
            <el-table-column
              label="已付金额"
              prop="paymentVendorMoney"
              width="200"
            ></el-table-column>
          </sc-table-select>
        </el-form-item>
        <el-form-item label="应付款金额" prop="money" class="priceItem">
          <el-input
            v-model="form.money"
            clearable
            placeholder="请输入付款金额"
            oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
            @blur="form.money = $event.target.value"
            @input="moneyChange"
          >
            <template #append>元</template></el-input
          >
          <!-- <div class="el-form-item-msg" v-if="form.type == 1">
           应付款金额：{{ form.money ?? 0 }}元
          </div> -->
        </el-form-item>
        <el-form-item
          label="本次付款金额"
          prop="thisTimeMoney"
          class="priceItem"
        >
          <el-input
            v-model="form.thisTimeMoney"
            clearable
            placeholder="请输入本次付款金额"
            oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
            @blur="form.thisTimeMoney = $event.target.value"
            @input="thisTimeMoneyChange"
          >
            <template #append>元</template></el-input
          >
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime">
          <el-date-picker
            v-model="form.payTime"
            type="date"
            placeholder="请选择付款日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="事由" prop="incident" style="width: 100%">
          <el-input
            v-model="form.incident"
            clearable
            type="textarea"
            rows="3"
            placeholder="请输入事由内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件">
          <sc-upload-file
            v-model="form.otherFileList"
            :limit="10"
            :multiple="false"
            tip="最多上传10个文件,单个文件不要超过20M"
          >
            <el-button type="primary" plain icon="el-icon-upload"
              >上传附件</el-button
            >
          </sc-upload-file>
        </el-form-item>
      </el-form>

      <!-- 审核流程项 -->
      <check-form-item
        ref="checkFormItem"
        @setCheckConfig="setCheckConfig"
        :field="this.form"
        :checkType="checkType"
        v-if="$route.query.mode == 'add'"
      ></check-form-item>

      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </div>
    <!-- 选择供应商 -->
    <!-- <select-vendor
			ref="selectVendor"
			@setVendor="setVendor"
		></select-vendor> -->
  </div>
</template>
<script>
import selectVendor from "@/components/selectVendor"
import checkFormItem from "@/components/checkProcess/checkFormItem.vue"
export default {
  name: "add",
  data () {
    return {
      checkType: 15, //工作流的类型
      isSaveing: false,
      selectContractLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        adminId: "", //跟进人id --登录时id
        departmentId: "",
        supplierId: "", //供应商id
        supplierName: "", //供应商
        // num: '',//付款编号
        title: "", //付款名称
        type: "", //付款金额
        money: "", //付款时间
        thisTimeMoney: "", //本次付款金额
        payTime: "", //备注
        exempt: "", //是否免审
        otherFileList: [], //附件
        incident: "",
        workflowJsonList: [],
        vendorData: {},//供应商数据
      },
      contractList: [], //合同列表
      vendorApiObj: this.$API.payment.getVendorList,
      vendorProps: {
        label: 'vendorName',
        value: 'vendorId',
      },
      //验证规则
      rules: {
        title: [{ required: true, message: "请输入付款名称" }],
        money: [{ required: true, message: "请输入付款金额" }],
        thisTimeMoney: [
          { required: true, message: "请输入本次付款金额" },
        ],
        payTime: [
          {
            required: true,
            message: "请输入付款时间",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择付款类型",
            trigger: "change",
          },
        ],
        supplierName: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "change",
          },
        ],
        vendorData: [{ required: true, message: "请选择供应商", trigger: "change" }],
      },
    }
  },
  components: {
    checkFormItem,
    selectVendor,
  },
  mounted () {
    // this.getContractList()
    if (this.$route.query.mode == "edit") {
      this.getLoanById()
    }
  },
  methods: {
    // 选择类型
    cahngeType () {
      this.form.money = ' '
      this.form.thisTimeMoney = ' '

    },
    //  选择供应商
    vendorChange (val) {
      this.form.money = val.needTotlaVendorMoney - val.paymentVendorMoney
      this.form.supplierId = val.vendorId
      console.log(this.form)
    },

    // 应付金额修改 
    moneyChange () {
      if (this.form.type == 1) {
        let val = this.form.vendorData.needTotlaVendorMoney - this.form.vendorData.paymentVendorMoney
        if (this.form.money > val) this.form.money = val
      }
    },
    // 本次付款金额修改
    thisTimeMoneyChange () {
      if (this.form.type == 1) {
        if (this.form.thisTimeMoney > this.form.money) this.form.thisTimeMoney = this.form.money
      }
    },

    // 审批流配置
    setCheckConfig (list, exempt) {
      this.form.workflowJsonList = list
      this.form.exempt = exempt
    },
    // 获取付款信息
    async getLoanById () {
      let { data } = await this.$API.payment.getPaymentById.get({
        id: this.$route.query.id,
      })
      this.form = data
      this.form.otherFileList.forEach((item) => {
        item.name = item.fileName
      })
    },

    // 选择供应商
    showSelectVendor () {
      this.$refs.selectVendor.open()
    },

    setVendor (data) {
      this.form.supplierName = data.name
      this.form.supplierId = data.id
    },

    async save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let userInfo = this.$TOOL.data.get("USER_INFO")
          this.form.departmentId = userInfo.departmentId
          this.form.adminId = userInfo.userId
          let fileType = ""
          this.form.otherFileList.map((item) => {
            if (item.url) {
              let arr = item.url.split(".")
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = "jpg/png/jpeg/svg"
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })
          let res = null
          let mode = this.$route.query.mode
          if (mode == "add") {
            res = await this.$API.payment.addPayment.post(
              this.form
            )
          } else if (mode == "edit") {
            delete this.form.status
            delete this.form.createTime
            delete this.form.updateTime
            res = await this.$API.payment.updatePayment.post(
              this.form
            )
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode)
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: "/finance/payment",
            })
            this.$message.success("操作成功")
          }
        }
      })
    },

    // 取消客户添加
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: "/finance/payment",
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;
  .el-form {
    width: 100%;
  }
}
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>
